package ;

import flash.display.Sprite;

import TileInfo;

/**
 * ...
 * @author m
 */

typedef Tiles1d = Array<TileInfo>;
typedef Tiles2d = Array<Tiles1d>;

	
class Scene extends Sprite
{	
	var mField: Tiles2d;
	var mRows: Int;
	var mCols: Int;
	
	var mClickPos: SceneCoord2d;
	var mClickAction: Bool;
	var mReleasePos: SceneCoord2d;
	var mReleaseAction: Bool;

	
	public function new() 
	{
		super();
		
		flash.Lib.current.addChild(this);		
		
		// prepare events
		mClickPos = new SceneCoord2d();
		mReleasePos = new SceneCoord2d();
		
		
		//Reset();
	}

	
	public function Reset()
	{		
		mRows = mCols = 0;
		
		mClickPos.Reset();
		mReleasePos.Reset();
		
		mReleaseAction = mClickAction = false;
	}
	
	
	public function CreateTileInfo(): TileInfo
	{
		return new TileInfo();
	}
	
	
	public function SetFieldSize(rows: Int, cols: Int)
	{
		mRows = rows;
		mCols = cols;
		
		mField = new Tiles2d();
		
		for (r in 0...mRows) {
			var oneRow: Tiles1d = new Tiles1d();
			for (c in 0...mCols) {
				oneRow.push(CreateTileInfo());
			}
			
			mField.push(oneRow);
		}
	}
	
	
	inline public function GetFieldRows(): Int
	{
		return mRows;
	}
	
	
	inline public function GetFieldCols(): Int
	{
		return mCols;
	}
	
	
	inline public function GetFieldClickPos(): SceneCoord2d
	{
		return mClickPos;
	}

	
	inline public function GetFieldReleasePos(): SceneCoord2d
	{
		return mReleasePos;
	}
	
	
	public function Start(void)
	{
		//trace("Starting...");
				
		StartNewLevel();
	}
	
	
	public function StartNewLevel()
	{
		//trace("Scene::StartNewLevel");
				
		Reset();
		
		CreateField();
		
		GenerateLevel();		
	}
	
	
	public function GenerateLevel()
	{
		//trace("Scene::GenerateLevel");
	}
	
	
	public function CreateField()
	{
		//trace("Scene::CreateField");
		
		// field
		mField = new Tiles2d();		
	}

	
	public function OnFieldClick(pos: SceneCoord2d);
	
	
	public function OnFieldRelease(pos: SceneCoord2d);

	
	public function UpdateLogic()
	{
		for (r in 0...mRows) {
			for (c in 0...mCols) {
				mField[r][c].Update();
			}
		}		
	}
}